package nuc.edu.cn.controller;

import nuc.edu.cn.pojo.Article;
import nuc.edu.cn.pojo.PageBean;
import nuc.edu.cn.pojo.Result;
import nuc.edu.cn.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @PostMapping
    public Result add(@Validated(Article.add.class) @RequestBody Article article){
      articleService.add(article);
      return Result.success();
  }
  @GetMapping
  public Result<PageBean<Article>> list(
          Integer pageNum,
          Integer pageSize,
          @RequestParam(required = false) Integer categoryId,
          @RequestParam(required = false) String state
  ){
       PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
       return Result.success(pb);
    }
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        Article article = articleService.detail(id);
        return Result.success(article);
    }
    @PutMapping
    public Result update(@Validated(Article.update.class) @RequestBody Article article){
        articleService.update(article);
        return Result.success();
    }
    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }

}
